﻿@namespace LaserMark.Components
@using LaserMark.DataAccess.Service

<div class="upload-temp">
    <ValidateForm Model="@Model" @ref="ValidateForm">
        <div class="row form-inline g-3">
            <div class="col-6">
                <Select @bind-Value="Model.Type" DisplayText="模板类型"></Select>
            </div>
            <div class="col-6">
                <BootstrapInput @bind-Value="Model.TemplateName" DisplayText="模板名称"></BootstrapInput>
            </div>
            <div class="col-6">
                <BootstrapInputNumber @bind-Value="Model.XMargin" DisplayText="模板宽"></BootstrapInputNumber>
            </div>
            <div class="col-6">
                <BootstrapInputNumber @bind-Value="Model.YMargin" DisplayText="模板高"></BootstrapInputNumber>
            </div>
            <div class="col-6">
                <BootstrapLabel Value="模板路径"></BootstrapLabel>
                <BootstrapInputGroup>
                    <BootstrapInput @bind-Value="Model.TemplatePath"></BootstrapInput>
                    <Button OnClick="OnClick" Text="上传"></Button>
                </BootstrapInputGroup>
            </div>
            <div class="col-6">
                <BootstrapInputNumber @bind-Value="Model.Order" DisplayText="标序"></BootstrapInputNumber>
            </div>
            @if (Model.Type == MarkType.QRcode)
            {
                <div class="col-6">
                    <MultiSelect Items="Items" @bind-Value="Model.MatchingNo" DisplayText="二维码显示字段" ShowLabelTooltip="true"></MultiSelect>
                </div>
            }
        </div>
    </ValidateForm>
</div>

<div class="form-footer">
    <DialogCloseButton></DialogCloseButton>
    <Button Icon="fa fa-save" Text="保存" OnClick="OnSave"></Button>
</div>

@code {
    [NotNull]
    private MarkTemplate? Model { get; set; } = new();

    [NotNull]
    private ValidateForm? ValidateForm { get; set; }

    [Inject]
    [NotNull]
    private ICodeTemplateService? CodeTemplateService { get; set; }

    [CascadingParameter]
    [NotNull]
    private Func<Task>? OnCloseAsync { get; set; }

    private List<SelectedItem> Items { get; set; } = new()
    {
        new SelectedItem(){ Value = "0",Text = "客户名称" },
        new SelectedItem(){ Value = "1",Text = "订单编号" },
        new SelectedItem(){ Value = "2",Text = "流程卡号" },
        new SelectedItem(){ Value = "3",Text = "小片宽" },
        new SelectedItem(){ Value = "4",Text = "小片高" },
        new SelectedItem(){ Value = "5",Text = "楼层号" },
        new SelectedItem(){ Value = "6",Text = "货架号" },
    };

    private void OnClick()
    {
        OpenFileDialog dialog = new OpenFileDialog();
        dialog.InitialDirectory = $@"{AppContext.BaseDirectory}";
        dialog.Filter = "模板文件|*.ez3";
        if (dialog.ShowDialog() == true)
        {
            Model.TemplatePath = $"{AppContext.BaseDirectory}\\Template\\{dialog.SafeFileName}";
            if (!File.Exists($"{AppContext.BaseDirectory}\\Template\\{dialog.SafeFileName}"))
            {
                File.Copy(dialog.FileName, $"{AppContext.BaseDirectory}\\Template\\{dialog.SafeFileName}");
            }
        }
    }

    private async Task OnSave()
    {
        var ret = ValidateForm.Validate();
        if (ret)
        {
            var obj = await CodeTemplateService.Insert(Model);
            if (obj)
            {
                await OnCloseAsync();
            }
        }
    }
}
